﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Connection;
using Connection.Presenter;
using Connection.IView;

namespace RemoChat
{
    /// <summary>
    /// Interaction logic for WinLogin.xaml
    /// </summary>
    public partial class WinLogin : Window, ILoginView
    {
        private string url="";
        private UserDB userDB;
        TcpChannel tcpChannel;

        public WinLogin()
        {
            InitializeComponent();
        }

        public WinLogin(string _URL)
        {
            InitializeComponent();
            url = _URL;
            userDB = (UserDB)Activator.GetObject(typeof(UserDB), url + "USERDB_OBJECT");
        }

        /*private void Button_Click(object sender, RoutedEventArgs e)
        {
            int kt;
            userDB = (UserDB)Activator.GetObject(typeof(UserDB), url + "USERDB_OBJECT");
            kt = userDB.InsertUser("11111", "test", "test");
            if (kt == -1)
                MessageBox.Show("Thêm Loại Nhân Viên Thất Bại", "Lỗi");
            else
            {
                MessageBox.Show("Thêm Loại Nhân Viên Thành Công", "Thông Báo");
            }
        }*/

        private void btnKetNoi_Click(object sender, RoutedEventArgs e)
        {
            if (txtIP.Text.Trim() == "" || txtPort.Text.Trim() == "")
                MessageBox.Show("Điền đầy đủ số IP và Post", "Thông Báo");
            else
            {
                try
                {
                    tcpChannel = new TcpChannel();
                    ChannelServices.RegisterChannel(tcpChannel, false);

                    url = "tcp://" + txtIP.Text + ":" + txtPort.Text + "/";

                    //tao the hien cua LOGINPRESENTER_OBJECT
                    RemotingConfiguration.RegisterWellKnownClientType(typeof(LoginPresenter), url + "PRESENTER/LOGINPRESENTER_OBJECT");
                    //tao the hien cua INVIPRESENTER_OBJECT
                    RemotingConfiguration.RegisterWellKnownClientType(typeof(InviPresenter), url + "PRESENTER/INVIPRESENTER_OBJECT");
                    //tao the hien cua CHATPRESENTER_OBJECT
                    RemotingConfiguration.RegisterWellKnownClientType(typeof(ChatPresenter), url + "PRESENTER/CHATPRESENTER_OBJECT");
                    //tao the hien cua SIGNUPPRESENTER_OBJECT
                    RemotingConfiguration.RegisterWellKnownClientType(typeof(SignUpPresenter), url + "PRESENTER/SIGNUPPRESENTER_OBJECT");

                    ChatPresenter chatPresenter = (ChatPresenter)Activator.GetObject(typeof(ChatPresenter), url + "PRESENTER/CHATPRESENTER_OBJECT");

                    if (chatPresenter.Test() != 0)
                    {
                        MessageBox.Show("Kết nối thất bại!\nKiểm tra lại IP hoặc Port", "Thông Báo");
                    }
                    else
                    {
                        gridDangNhap.IsEnabled = true;
                        btnDangNhap.IsEnabled = true;
                        btnDangKy.IsEnabled = true;

                        gridKetNoi.IsEnabled = false;
                        btnKetNoi.IsEnabled = false;
                    }
                }
                catch(Exception ex)
                {
                    //MessageBox.Show("Kết nối thất bại!\nKiểm tra lại IP hoặc Port", "Thông Báo");
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void btnDangNhap_Click(object sender, RoutedEventArgs e)
        {
            if (this.Username.Trim() == "" || this.Password.Trim() == "")
                MessageBox.Show("Phải nhập đầy đủ Username và Password", "Thông báo");
            else
            {
                LoginPresenter loginPresenter = (LoginPresenter)Activator.GetObject(typeof(LoginPresenter), url + "PRESENTER/LOGINPRESENTER_OBJECT");
                if (loginPresenter.IsLoginOk(this.Username, this.Password))
                {
                    //WinInviChat wininvichat = new WinInviChat(url, this.Username);
                    //wininvichat.Show();
                    WinOnline winonline = new WinOnline(url, this.Username);
                    winonline.Show();
                    loginPresenter.UpdateIsOnline(this.Username, 1);
                    this.Hide();
                }
                else
                    MessageBox.Show("Đăng nhập thất bại", "Thông báo");
            }
        }

        private void btnThoat_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnDangKy_Click(object sender, RoutedEventArgs e)
        {
            WinSignUp winsignup = new WinSignUp(url);
            winsignup.Show();
        }

        public string Username
        {
            get { return txtUsername.Text; }
            set { txtUsername.Text = value; }
        }

        public string Password
        {
            get { return pwdPassword.Password.ToString(); }
            set { pwdPassword.Password = value; }
        }

        private void txtIP_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                btnKetNoi_Click(sender, e);
        }

        private void txtUsername_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                btnDangNhap_Click(sender, e);
        }
    }
}
